﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;

namespace RestaurantManagerWebApp.Pages
{
    public partial class Main : System.Web.UI.Page
    {
        string connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //check for session state and populate welcome label
                if (Session["Username"] == null)
                {
                    Response.Redirect("~/Pages/UserManagement/UserLogin.aspx");
                }
                else
                {
                    String uid = Session["Username"].ToString();
                    SqlConnection conn = new SqlConnection(connection);
                    conn.Open();
                    string getName = "SELECT fname FROM Emp_records WHERE id_no=@uid";
                    SqlCommand cmd1 = new SqlCommand(getName, conn);
                    cmd1.Parameters.AddWithValue("@uid", uid);
                    SqlDataReader dr2 = cmd1.ExecuteReader();
                    while (dr2.Read())
                    {
                        string name = dr2["fname"].ToString();
                        lblWelcome.Text = "Welcome, " + name;
                    }
                    dr2.Close();

                    int rID = 1;
                    getRes(conn, rID);
                    getResPhoto(conn, rID);
                }

                

            }
            else
            {

            }
            
            

        }

        private void getRes(SqlConnection conn, int rID)
        {
            string getRes = "Select r_name, address, postal_code, tel_no, email, brn, url FROM Restaurant where r_id=@rID";
            SqlCommand cmd2 = new SqlCommand(getRes, conn);
            cmd2.Parameters.AddWithValue("@rID", rID);
            SqlDataReader dr3 = cmd2.ExecuteReader();
            while (dr3.Read())
            {
                txtAddr.Text = dr3["address"].ToString();
                txtResName.Text = dr3["r_name"].ToString();
                txtPosCode.Text = dr3["postal_code"].ToString();
                txtTel.Text = dr3["tel_no"].ToString();
                txtEmail.Text = dr3["email"].ToString();
                txtBRN.Text = dr3["brn"].ToString();
                txtUrl.Text = dr3["url"].ToString();
            }
            dr3.Close();
        }

        private void getResPhoto(SqlConnection conn, int rID)
        {
            string getResPhoto = "Select pathname FROM Photos where r_id=@rID";
            SqlCommand cmd3 = new SqlCommand(getResPhoto, conn);
            cmd3.Parameters.AddWithValue("@rID", rID);
            SqlDataReader dr4 = cmd3.ExecuteReader();
            while (dr4.Read())
            {
                Image1.ImageUrl = dr4["pathname"].ToString();
            }
            dr4.Close();
        }

        
        
        /*Menu Management
         * 21/06/2014 Ingrid
         */
        protected void btnMenuManagement_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/MenuManagement/MainMenuManagement.aspx");
        }

        /*Menu Management
        * 21/06/2014 Ingrid
        */
        protected void btnOrderManagement_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/OrderManagement/MainOrderManagement.aspx");
        }

        /*Stock Management
         * 21/06/2014 Ingrid
         */
        protected void btnStockManagement_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/StockManagement/MainStockManagement.aspx");
        }

        /*User Management
         * 21/06/2014 Ingrid
         */
        protected void btnUserManagement_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/UserManagement/ManagementHome.aspx");
        }

        /*Table Management
         * 21/06/2014 Ingrid
         */
        protected void btnTableManagement_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/TableManagement/MainTableManagement.aspx");
        }

        /*Report Management
        * 21/06/2014 Ingrid
        */
        protected void btnReports_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Reports/MainReportManagement.aspx");
        }

        protected void lblLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("~/Pages/UserManagement/UserLogin.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            
            string rname = txtResName.Text;
            string addr = txtAddr.Text;
            string posCode = txtPosCode.Text;
            string tel = txtTel.Text;
            string email = txtEmail.Text;
            string brn = txtBRN.Text;
            string url = txtUrl.Text;
            int rID = 1;
            SqlConnection conn = new SqlConnection(connection);
            conn.Open();

            UpdateRestaurant(rname, addr, posCode, tel, email, brn, url, conn,rID);
            if (Image1.ImageUrl != null)
            {
                string uploadFolderPath = Image1.ImageUrl.ToString();
                UpdatePhotoRecord(conn, rname, uploadFolderPath, rID);
                Response.Write("<script type=\"text/javascript\">alert('Restaurant Information Successfully Updated!');</script>");
            }
            
            
            conn.Close();
        }


        private static void UpdateRestaurant(string rname, string addr, string posCode, string tel, string email, string brn, string url, SqlConnection conn, int rID)
        {
            string update = "UPDATE Restaurant SET r_name=@rname, address=@addr, postal_code=@posCode, tel_no=@tel, email=@email, brn=@brn, url=@url where r_id=@rID";
            SqlCommand cmd = new SqlCommand(update, conn);
            cmd.Parameters.AddWithValue("@rname", rname);
            cmd.Parameters.AddWithValue("@addr", addr);
            cmd.Parameters.AddWithValue("@posCode", posCode);
            cmd.Parameters.AddWithValue("@tel", tel);
            cmd.Parameters.AddWithValue("@email", email);
            cmd.Parameters.AddWithValue("@brn", brn);
            cmd.Parameters.AddWithValue("@url", url);
            cmd.Parameters.AddWithValue("@rID", rID);
            cmd.ExecuteNonQuery();
        }



        private void UpdatePhotoRecord(SqlConnection conn, string pname, string pathname, int rID)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "UPDATE Photos SET p_name=@pname, pathname=@pathname WHERE r_id=@rID";
                cmd.Parameters.AddWithValue("@pname", pname);
                cmd.Parameters.AddWithValue("@rID", rID);
                cmd.Parameters.AddWithValue("@pathname", pathname);
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtAddr.Text = "";
            txtBRN.Text = "";
            txtEmail.Text = "";
            txtPosCode.Text = "";
            txtResName.Text = "";
            txtTel.Text = "";
            txtUrl.Text = "";
        }

        protected void btnUpload_Click1(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string fileName = FileUpload1.FileName.ToString();
                string uploadFolderPath = "~/Images/";
                string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
                FileUpload1.SaveAs(filePath + "\\" + fileName);
                Image1.ImageUrl = "~/Images/" + "/" + FileUpload1.FileName.ToString();

            }
        }

    }
}